#include <sobjc-commons/defines.h>
#include <sobjc-commons/str_list.h>
#include <sobjc-commons/st.h>
#include <sobjc-commons/builtin_sels.h>
#include "sobjc/selector.h"
#include "selector.h"

static st_table *sel_table;

void
objc_init_sel()
{
  sel_table = st_init_strtable();
}

SEL
objc_register_sel(const char *name)
{
  SEL sel;
  size_t i;
  struct objc_builtin_selector *bsel;

  if ((bsel = objc_get_builtin_sel(name)) != NULL)
    return (SEL)bsel;

  sel = objc_get_sel(name);
  if (sel != NULL) {
    return sel;
  } else {
    sel = ALLOC(struct objc_selector);
    sel->name = objc_strdup(name);
    sel->nargs = 0;
#ifdef OBJC_PROFILE
    sel->send_count = 0;
#endif /* OBJC_PROFILE */
    for (i = 0; i < strlen(name); i++)
      if (name[i] == ':')
        sel->nargs++;
	st_add_direct(sel_table, (st_data_t)sel->name, (st_data_t)sel);
    return sel;
  }
}

SEL
objc_get_sel(const char *name)
{
  SEL sel = NULL;
  struct objc_builtin_selector *bsel;

  if ((bsel = objc_get_builtin_sel(name)) != NULL)
    return (SEL)bsel;

  st_lookup(sel_table, (st_data_t)name, (st_data_t *)&sel);
  return sel;
}

const char *
objc_sel_get_name(SEL sel)
{
  return sel->name;
}

int
objc_sel_get_nargs(SEL sel)
{
  return sel->nargs;
}

